package com.mercycrazy.recruit.util;

/**
 * 通用脱敏工具类, 可用于: 用户名、手机号、邮箱、地址等
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @see cn.hutool.core.util.DesensitizedUtil
 * @since 2023-01-14
 */
@SuppressWarnings("all")
public class DesensitizationUtil {

    private static final int SIZE = 6;
    private static final String SYMBOL = "*";

    /**
     * 通用脱敏方法
     */
    public static String commonDisplay(String value) {
        if (null == value || "".equals(value)) {
            return value;
        }
        int len = value.length();
        int pamaone = len / 2;
        int pamatwo = pamaone - 1;
        int pamathree = len % 2;
        StringBuilder stringBuilder = new StringBuilder();
        if (len <= 2) {
            if (pamathree == 1) {
                return SYMBOL;
            }
            stringBuilder.append(SYMBOL);
            stringBuilder.append(value.charAt(len - 1));
        } else {
            if (pamatwo <= 0) {
                stringBuilder.append(value.charAt(0));
                stringBuilder.append(SYMBOL);
                stringBuilder.append(value.charAt(len - 1));

            } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
                int pamafive = (len - SIZE) / 2;
                stringBuilder.append(value, 0, pamafive);
                for (int i = 0; i < SIZE; i++) {
                    stringBuilder.append(SYMBOL);
                }
                stringBuilder.append(value, len - (pamafive + 1), len);
            } else {
                int pamafour = len - 2;
                stringBuilder.append(value.charAt(0));
                for (int i = 0; i < pamafour; i++) {
                    stringBuilder.append(SYMBOL);
                }
                stringBuilder.append(value.charAt(len - 1));
            }
        }
        return stringBuilder.toString();
    }
}
